#include "ObCloud.h"
#include "D3DApp.h"

IDirect3DTexture9* ObCloud::mTex = 0;

ObCloud::ObCloud(){
	setId(Cloud1);
	mInfo	= 0;
}

ObCloud::ObCloud(CLOUD nid){
	setId(nid);
	mInfo	= 0;
}

ObCloud::~ObCloud(){
	
}

bool ObCloud::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 48.0f;
		mHeight = 32.0f;
		mSpritePos.x = 30;
		switch (mID)
		{
		case Cloud2:
			mSpritePos.y = 60;
			break;
		default:
			mSpritePos.y = 20;
			break;
		}
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObCloud::checkType(int obID){
	if(obID<Cloud1 || obID>Cloud2){
		return false;
	}else{
		return true;
	}
}

void ObCloud::update(float dt){

}

void ObCloud::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y - gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false);
}

bool ObCloud::loadTexture(){
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/Cloud.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
		return true;
	}else{
		return false;
	}
}

bool ObCloud::releaseTexture(){
	if(mTex != 0){
		ReleaseCOM(mTex);
		return true;
	}else{
		return false;
	}
}